package com.shanzhu.st.controller;

import com.shanzhu.st.entity.Order;
import com.shanzhu.st.enums.ErrorMsg;
import com.shanzhu.st.service.OrderService;
import com.shanzhu.st.utils.IdFactoryUtil;
import com.shanzhu.st.utils.OrderTaskHandler;
import com.shanzhu.st.vo.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 订单 控制层
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 添加订单
    */
    @PostMapping("/add")
    @CrossOrigin
    public R addOrder(
        @CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录") @NotEmpty(message = "登录异常 请重新登录") String shUserId,

        @RequestBody Order order) {
        order.setOrderNumber(System.currentTimeMillis()+"");

        order.setAccountNumber(shUserId);

        if (orderService.addOrder(order) == 1) {
            return R.success(order);
        }
        return R.fail(ErrorMsg.SYSTEM_ERROR);
    }

    /**
     * 更新订单信息
     */
    @PostMapping("/update")
    public R updateOrder(
        @CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录") @NotEmpty(message = "登录异常 请重新登录") String shUserId,
        @RequestBody Order order) {
        return R.fail(ErrorMsg.SYSTEM_ERROR);
    }

    /**
     * 获取我的订单
     */
    @GetMapping("/my")
    public R getMyOrder(
        @CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录") @NotEmpty(message = "登录异常 请重新登录") String shUserId) {
        return R.success(orderService.getMyOrder(Long.valueOf(shUserId)));
    }

}
